What is indent-string?
The indent-string npm package is a utility that allows users to add indentation to the beginning of each line in a string. This can be particularly useful for formatting multi-line strings or code snippets in a more readable manner. The package provides a simple interface to specify the number of spaces or the string to use for indentation, making it a versatile tool for text manipulation in Node.js applications.
What are indent-string's main functionalities?
Indenting with spaces
This feature allows you to indent each line of a given string with a specified number of spaces. In the code sample, the string 'Unicorn\nRainbow' is indented by 4 spaces.
const indentString = require('indent-string');
console.log(indentString('Unicorn\nRainbow', 4));
// Output:
// Unicorn
// Rainbow
Indenting with custom string
This feature enables indenting each line of a string with a custom string instead of spaces. In the example, the string 'Unicorn\nRainbow' is indented using the '♥' character, repeated 4 times as specified.
const indentString = require('indent-string');
console.log(indentString('Unicorn\nRainbow', 4, {indent: '♥'}));
// Output:
// ♥♥♥♥Unicorn
// ♥♥♥♥Rainbow
Other packages similar to indent-string
left-pad
Similar to indent-string, left-pad adds characters to the left of a string, effectively indenting it. However, left-pad is more focused on padding a single line to a specific length, rather than indenting all lines in a multi-line string.
cli-boxes
While not directly similar, cli-boxes is a package that deals with text formatting in the terminal, offering a variety of box styles for CLI applications. It can be used in conjunction with indent-string for more complex text formatting tasks.
indent-string
Indent each line in a string
Install
$ npm install indent-string
Usage
const indentString = require('indent-string');
indentString('Unicorns\nRainbows', 4);
indentString('Unicorns\nRainbows', 4, {indent: '♥'});
API
indentString(string, [count], [options])
string
Type: string
The string to indent.
count
Type: number
Default: 1
How many times you want options.indent
repeated.
options
Type: object
indent
Type: string
Default: ' '
The string to use for the indent.
includeEmptyLines
Type: boolean
Default: false
Also indent empty lines.
Related
License
MIT © Sindre Sorhus